package com.ray.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.ray.bean.MaterialTypeExceptSourceBean;
import com.ray.bean.OrderCustomTagBean;
import com.ray.bean.OrderInfoDetailBean;
import com.ray.bean.OrderInfoTemplateBean;
import com.ray.exception.ServiceException;
import com.ray.mapper.BasicDataMapper;
import com.ray.mapper.FileManagerMapper;
import com.ray.mapper.MaterialTypeExceptSourceMapper;
import com.ray.mapper.OrderCreateApplyMapper;
import com.ray.mapper.OrderCreateBusinessMapper;
import com.ray.mapper.OrderCreateMapper;
import com.ray.util.ArrayUtils;
/**
 * 
 * @author Shensg
 * 2018年1月26日
 */
@Service
public class OrderCreateBusinessService {
	@Autowired
	OrderCreateBusinessMapper orderCreateMapper;
	@Autowired
	OrderCreateApplyService orderCreateApplyService;
	@Autowired
	BasicDataMapper basicDataMapper;
	@Autowired
	FileManagerMapper fileManagerMapper;
	@Autowired
	MaterialTypeExceptSourceMapper materialTypeExceptSouceMapper;
	
	public List<Map> getOrderTabList(Map map) {
		String type = map.get("type").toString();
		if("1".equals(type)) {
			map.remove("type");
			map.put("type","原材料仓");
			//return basicDataMapper.getOrderTabList(map);
			return basicDataMapper.getOrderTabListForSourceFactory(map);
		}
		if("2".equals(type)) {
			map.remove("type");
			map.put("type","辅料仓");
			return basicDataMapper.getOrderTabList(map);
		}
		if("3".equals(type)) {
			map.remove("type");
			map.put("type","信息备注仓");
			MaterialTypeExceptSourceBean bean = new MaterialTypeExceptSourceBean();
			bean.setCompany(map.get("loginCompany").toString());
			bean.setType("3");
			return materialTypeExceptSouceMapper.getListForCreateOrder(bean);
		}
		return orderCreateMapper.getOrderTabList(map);
	}

	public List<Map> getFactoryTabChildList(Map<String, String> map) {
		// TODO Auto-generated method stub
		if("1".equals(map.get("material_factory"))){
			map.remove("material_factory");
			map.put("material_factory","原材料仓");
		}
		if("2".equals(map.get("material_factory"))){
			map.remove("material_factory");
			map.put("material_factory","辅料仓");
		}
		if("3".equals(map.get("material_factory"))){
			map.remove("material_factory");
			map.put("material_factory","信息备注仓");
		}
		return orderCreateMapper.getFactoryTabChildList(map);
	}
	//保存html 模版
	public int saveAsOrderTemplate(OrderInfoTemplateBean bean) {
		// TODO Auto-generated method stub
		return orderCreateMapper.saveAsOrderTemplate(bean);
	}
	//获取基础数据列表
	public List<Map> getDataBasicList(Map map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getDataBasicList(map);
	}

	public void updateDataBasicById(Map map) {
		// TODO Auto-generated method stub
		String str = map.get("html_type_val").toString().replaceAll("，", ",");
		map.put("html_type_val", str);
		orderCreateMapper.updateDataBasicById(map);
	}
	
	//保存订单信息
	public boolean saveOrderInfo(Map map) throws ServiceException {
		String save_as_new_order=map.get("save_as_new_order").toString();
		String order_no = map.get("order_p_order_no").toString();
		if("1".equals(save_as_new_order)) {
			order_no=(String) orderCreateMapper.getOrderInfoFixed(map).get("result");
			fileManagerMapper.updateLastOneOrderNoByUserName(order_no,map);
		}
		map.put("order_p_order_no",order_no);
		if(map.get("order_p_audit")!=null) {
			if("true".equals(map.get("order_p_audit").toString())) {
				map.put("order_p_audit", "1");
			}else {
				map.put("order_p_audit", "0");
			}
		}
		if(map.get("order_p_order_no_comp")!=null) {
			//客户订单编号 如果没有 和订单编号一样 ，有取自己的
			if("".equals(map.get("order_p_order_no_comp").toString().trim())) {
				map.put("order_p_order_no_comp", order_no);
			}
		}
		//订单提交 审核不选 订单状态默认计划
		map.put("order_p_order_status", "计划");
		map.put("order_p_audit", "0");
		
		map.put("audit_type", "1");//生产订单审核 项目为1 
		/*if(orderCreateMapper.checkAuditTheSameOne(map)>0) {
			map.put("order_p_order_status", "审核");
			map.put("order_p_audit", "1");
		}*/
		
		String order_template_id = map.get("order_template_id").toString();
		//删除原有订单信息 生产订单主表, order_info_detail order_info_tail order_custom_tag
		int id = orderCreateMapper.deleteOrderInfo(order_no);
		//保存生产订单主表
		int id_primary = orderCreateMapper.saveOrderInfo(map);
		//保存生产订单明细表
		List<OrderInfoDetailBean> list_orderDetails = getOrderInfoDetailBean(map,order_no);
		//保存自定义表单
		List<OrderCustomTagBean> list_orderCustomTag = getOrderCustomTagBean(map,order_no);
		
		//保存生产订单明细表 数据过大拆分
		List<OrderInfoDetailBean> list_orderDetails_new= new ArrayList<>();
		for (int i = 0; i < list_orderDetails.size(); i++) {
			list_orderDetails_new.add(list_orderDetails.get(i));
			if(list_orderDetails_new.size()==20) {//每20条数据拆分
				int id_detail = orderCreateMapper.saveOrderInfoDetail(list_orderDetails_new);
				list_orderDetails_new.clear();
			}
		}
		if(list_orderDetails_new.size()>0) {//如果少于20条
			int id_detail = orderCreateMapper.saveOrderInfoDetail(list_orderDetails_new);
			list_orderDetails_new.clear();
		}
		//保存生产订单明细-tail表
		List<OrderInfoDetailBean> list_orderTails = getOrderInfoTailBean(map,order_no);
		int id_tail = orderCreateMapper.saveOrderInfoTail(list_orderTails);
		//当投料数大于0保存一份到order_info_detail
		orderCreateMapper.saveOrderInfoDetailFromTail(order_no,map.get("order_p_order_total").toString());
		//保存自定义标签
		int id_customTag = orderCreateMapper.saveOrderCustomTag(list_orderCustomTag);
		//保存模版和订单编号
		if(Integer.valueOf(order_template_id)!=0) {
			orderCreateMapper.deleteOrderInfoAndTemplate(order_no);
			int id_order_template = orderCreateMapper.saveOrderInfoAndTemplate(order_no,order_template_id);
		}
		//插入companyMaterial 不再保存20180404
		/*for(OrderInfoDetailBean bean:list_orderDetails) {
			if(bean.getMaterialFactory()=="1") {
				bean.setMaterialFactory("原材料仓");
			}else if(bean.getMaterialFactory()=="2") {
				bean.setMaterialFactory("辅料仓");
			}else {
				bean.setMaterialFactory("信息备注仓");
			}
			bean.setCompany(map.get("loginCompany").toString());
			if(bean.getMaterialCode()!=null&&bean.getMaterialCode().trim().length()!=0) {//有code insert into select
				orderCreateMapper.insertCompanyMTbyCode(bean.getMaterialCode(),bean.getCompany());
			}else {
				orderCreateMapper.insertCompanyMTbyBean(bean);
			}
		}
		for(OrderInfoDetailBean bean:list_orderTails) {
			if(bean.getMaterialFactory()=="1") {
				bean.setMaterialFactory("原材料仓");
			}else if(bean.getMaterialFactory()=="2") {
				bean.setMaterialFactory("辅料仓");
			}else {
				bean.setMaterialFactory("信息备注仓");
			}
			bean.setCompany(map.get("loginCompany").toString());
			if(bean.getMaterialCode()!=null&&bean.getMaterialCode().trim().length()!=0) {//有code insert into select
				orderCreateMapper.insertCompanyMTbyCode(bean.getMaterialCode(),bean.getCompany());
			}else {
				orderCreateMapper.insertCompanyMTbyBean(bean);
			}
		}*/
		//推送重写 //推动给订单关联的
		//map.put("order_no",order_no);
		//orderCreateApplyService.sendTemplateMsg("apply",map);
		return true;
	}
	
	//获取自定义表单信息数据 组装 当前只有一条数据
	private List<OrderCustomTagBean> getOrderCustomTagBean(Map map, String order_no) {
		List<OrderCustomTagBean> list = new ArrayList<>();
		OrderCustomTagBean bean =new OrderCustomTagBean();
		if(map.get("order_label_label_val")!=null) {
			bean.setLabel(map.get("order_label_label_val").toString());
		}
		if(map.get("order_label_table_val")!=null) {
			bean.setTable(map.get("order_label_table_val").toString());
		}
		if(map.get("order_label_table_val_two")!=null) {
			bean.setTableTwo(map.get("order_label_table_val_two").toString());
		}
		if(map.get("order_label_table_val_three")!=null) {
			bean.setTableThree(map.get("order_label_table_val_three").toString());
		}
		if(map.get("order_label_table_val_four")!=null) {
			bean.setTableFour(map.get("order_label_table_val_four").toString());
		}
		if(map.get("order_label_table_val_five")!=null) {
			bean.setTableFive(map.get("order_label_table_val_five").toString());
		}
		//6
		if(map.get("order_label_table_val_six")!=null) {
			bean.setTableSix(map.get("order_label_table_val_six").toString());
		}
		//7
		if(map.get("order_label_table_val_seven")!=null) {
			bean.setTableSeven(map.get("order_label_table_val_seven").toString());
		}
		//8
		if(map.get("order_label_table_val_eight")!=null) {
			bean.setTableEight(map.get("order_label_table_val_eight").toString());
		}
		//9
		if(map.get("order_label_table_val_nine")!=null) {
			bean.setTableNine(map.get("order_label_table_val_nine").toString());
		}
		//10
		if(map.get("order_label_table_val_ten")!=null) {
			bean.setTableTen(map.get("order_label_table_val_ten").toString());
		}
		bean.setOrderNo(order_no);
		list.add(bean);
		return list;
	}

	//获取订单明细尾巴Tail表
	private List<OrderInfoDetailBean> getOrderInfoTailBean(Map map, String orderNo) {
		List<OrderInfoDetailBean> list = new ArrayList<>();
		OrderInfoDetailBean bean = null;
		if(map.get("order_s_input_material_house")==null) {
			return list;
		}
		String[] materialHouse = map.get("order_s_input_material_house").toString().split(",");
		String[] materialType=map.get("order_s_input_material_type").toString().split(",");
		String[] materialModel=map.get("order_s_input_val_material_type").toString().split(",");
		String[] materialCode=map.get("order_s_input_material_code").toString().split(",");
		String[] feedTotal=null;
		if(map.get("order_s_input_val_feed_total")!=null)feedTotal=map.get("order_s_input_val_feed_total").toString().split(",");
		
		for(int i=0;i<materialType.length;i++) {
			bean = new OrderInfoDetailBean();
			bean.setMaterialFactory(materialHouse[i]);
			bean.setMaterialType(materialType[i]);
			bean.setOrderNo(orderNo);
			bean.setMaterialModel(materialModel[i]);
			bean.setMaterialCode(materialCode[i]);
			if(feedTotal!=null) {
				bean.setFeedTotal((feedTotal[i].trim().length()>0)?feedTotal[i].trim():"0");//投料数
			}
			list.add(bean);
			System.out.println(bean.toString());
		}
		return list;
	}
	
	//获取订单明细Bean
	private List<OrderInfoDetailBean> getOrderInfoDetailBean(Map map,String orderNo) {
		List<OrderInfoDetailBean> list = new ArrayList<>();
		OrderInfoDetailBean bean = null;
		if(map.get("order_s_table_source_arr_str")==null) {
			return list;
		}
		/*投料数*/
		String[] feedTotal = null;
		if(map.get("order_s_feed_total")!=null)feedTotal=map.get("order_s_feed_total").toString().split(",");
		/*行号*/
		String[] lineNo	= map.get("order_s_line_no").toString().split(",");
		/*物料代码*/
		String[] materialCode	= null;
		if(map.get("order_s_material_code")!=null)materialCode=map.get("order_s_material_code").toString().split(",");
		/*色号*/
		String[] materialColor= null;
		if(map.get("order_s_material_color")!=null)materialColor=map.get("order_s_material_color").toString().split(",");
		/*色号描述*/
		String[] materialColorDescription = null;
		if(map.get("order_s_material_description")!=null)materialColorDescription=map.get("order_s_material_description").toString().split(",");
		/*规格*/
		String[] materialModel=null;
		if(map.get("order_s_material_model")!=null)materialModel= map.get("order_s_material_model").toString().split(",");
		/*物料类型*/
		String[] materialType=null;
		if(map.get("order_s_material_type")!=null) materialType	=map.get("order_s_material_type").toString().split(",");
		/*订单数*/
		String[] orderTotal	= null;
		if(map.get("order_s_order_total")!=null)orderTotal=map.get("order_s_order_total").toString().split(",");
		/*物料所属仓*/
		String[] materialFactory=null;
		if(map.get("order_s_material_house")!=null)materialFactory=map.get("order_s_material_house").toString().split(",");

		/*每列有几个子列*/
		String[] tableSourceArr	=null;
		if(map.get("order_s_table_source_arr")!=null)tableSourceArr=map.get("order_s_table_source_arr").toString().split(",");
		/*order_s_mtlkt_total	每条料可开数*/
		String[] mtlktTotal	=null;
		if(map.get("order_s_mtlkt_total")!=null)mtlktTotal=map.get("order_s_mtlkt_total").toString().split(",");
		/*order_s_klfs_way	开料方式*/
		String[] klfsWay	=null;
		if(map.get("order_s_klfs_way")!=null)klfsWay=map.get("order_s_klfs_way").toString().split(",");
		/*order_s_ylts_total	用料条数*/
		String[] yltsTotal	=null;
		if(map.get("order_s_ylts_total")!=null)yltsTotal=map.get("order_s_ylts_total").toString().split(",");
		/*order_s_klcc_size	开料尺寸*/
		String[] klccSize	=null;
		if(map.get("order_s_klcc_size")!=null)klccSize=map.get("order_s_klcc_size").toString().split(",");
		/*order_s_small_order_total	每列订单数*/
		String[] smallOrderTotal	=null;
		if(map.get("order_s_small_order_total")!=null)smallOrderTotal=map.get("order_s_small_order_total").toString().split(",");
		/*order_s_small_feed_total	每列投料数*/
		String[] smallFeedTotal	=null;
		if(map.get("order_s_small_feed_total")!=null)smallFeedTotal=map.get("order_s_small_feed_total").toString().split(",");
		//拼料颜色
		String[] plys=null;
		if(map.get("order_s_plys")!=null)plys=map.get("order_s_plys").toString().split(",");
		//SKU号
		String[] skuNo=null;
		if(map.get("order_s_sku_no")!=null)skuNo=map.get("order_s_sku_no").toString().split(",");
		//单只条码贴
		String[] singleCode=null;
		if(map.get("order_s_single_code")!=null)singleCode=map.get("order_s_single_code").toString().split(",");
		//内盒商品标识贴
		String[] nhspCode=null;
		if(map.get("order_s_nhsp_code")!=null)nhspCode=map.get("order_s_nhsp_code").toString().split(",");
		//外箱商品标识贴
		String[] wxspCode=null;
		if(map.get("order_s_wxsp_code")!=null)wxspCode=map.get("order_s_wxsp_code").toString().split(",");
		//客户PO号
		String[] custPoNo=null;
		if(map.get("order_s_cust_po_no")!=null)custPoNo=map.get("order_s_cust_po_no").toString().split(",");
		//说明
		String[] remark=null;
		if(map.get("order_s_remark")!=null)remark=map.get("order_s_remark").toString().split(",");
		//镜片材质
		String[] glassesType=null;
		if(map.get("order_s_glasses_type")!=null)glassesType=map.get("order_s_glasses_type").toString().split(",");
		//透光率
		String[] lightTrans=null;
		if(map.get("order_s_light_trans")!=null)lightTrans=map.get("order_s_light_trans").toString().split(",");
		//位置
		String[] location=null;
		if(map.get("order_s_location")!=null)location=map.get("order_s_location").toString().split(",");
		//颜色
		String[] otherColor=null;
		if(map.get("order_s_other_color")!=null)otherColor=map.get("order_s_other_color").toString().split(",");
		//内容
		String[] content=null;
		if(map.get("order_s_content")!=null)content=map.get("order_s_content").toString().split(",");
		//尺寸
		String[] size=null;
		if(map.get("order_s_size")!=null)size=map.get("order_s_size").toString().split(",");
		//材质
		String[] qualityType=null;
		if(map.get("order_s_quality_type")!=null)qualityType=map.get("order_s_quality_type").toString().split(",");
		//字体
		String[] font=null;
		if(map.get("order_s_font")!=null)font=map.get("order_s_font").toString().split(",");
		//工艺
		String[] arts=null;
		if(map.get("order_s_arts")!=null)arts=map.get("order_s_arts").toString().split(",");

		String order_s_table_source_arr_str=map.get("order_s_table_source_arr_str").toString();
		List<List<String[]>> listArrStrList=ArrayUtils.arrayStrToArrayObj(order_s_table_source_arr_str);
		boolean addlist=true;
		for(int materialTypeIndex=0;materialTypeIndex<listArrStrList.size();materialTypeIndex++) {
			List<String[]> listArrStr = listArrStrList.get(materialTypeIndex);
			for (int i = 0; i < lineNo.length; i++) { //几行
				for (int j = 0; j < listArrStr.size(); j++) { //每列
					bean = new OrderInfoDetailBean();
					for (int j2 = 0; j2 < listArrStr.get(j).length; j2++) {//有几个小列
						switch (listArrStr.get(j)[j2].replaceAll("'", "")) {//每个小列是什么
							case "order_s_material_color"://有色号就有code
								bean.setColor(materialColor[0]);materialColor=ArrayUtils.delete(0, materialColor);
								bean.setMaterialCode(materialCode[0]);materialCode=ArrayUtils.delete(0, materialCode);	
								break;
							case "order_s_material_description":
								bean.setColorDescription(materialColorDescription[0]);materialColorDescription=ArrayUtils.delete(0, materialColorDescription);
								break;
							case "order_s_material_model":
								bean.setMaterialModel(materialModel[0]);materialModel=ArrayUtils.delete(0, materialModel);
								break;
							case "order_s_mtlkt_total":
								bean.setMtlktTotal(mtlktTotal[0]);mtlktTotal=ArrayUtils.delete(0, mtlktTotal);
								break;
							case "order_s_klfs_way":
								bean.setKlfsWay(klfsWay[0]);klfsWay=ArrayUtils.delete(0, klfsWay);
								break;
							case "order_s_small_order_total":
								if(smallOrderTotal[0].trim().length()==0) {
									bean.setSmallOrderTotal(null);smallOrderTotal=ArrayUtils.delete(0, smallOrderTotal);
								}else {
									bean.setSmallOrderTotal(smallOrderTotal[0]);smallOrderTotal=ArrayUtils.delete(0, smallOrderTotal);
								}
								break;
							case "order_s_small_feed_total":
								if(smallFeedTotal[0].trim().length()==0) {
									bean.setSmallFeedTotal(null);smallFeedTotal=ArrayUtils.delete(0, smallFeedTotal);
								}else {
									bean.setSmallFeedTotal(smallFeedTotal[0]);smallFeedTotal=ArrayUtils.delete(0, smallFeedTotal);
								}
								break;
							case "order_s_klcc_size":
								bean.setKlccSize(klccSize[0]);klccSize=ArrayUtils.delete(0, klccSize);
								break;
							case "order_s_ylts_total":
								bean.setYltsTotal(yltsTotal[0]);yltsTotal=ArrayUtils.delete(0, yltsTotal);
								break;
							case "order_s_plys":
								bean.setPlys(plys[0]);plys=ArrayUtils.delete(0, plys);
								break;
							case "order_s_sku_no":
								bean.setSkuNo(skuNo[0]);skuNo=ArrayUtils.delete(0, skuNo);
								break;
							case "order_s_single_code":
								bean.setSingleCode(singleCode[0]);singleCode=ArrayUtils.delete(0, singleCode);
								break;
							case "order_s_nhsp_code":
								bean.setNhspCode(nhspCode[0]);nhspCode=ArrayUtils.delete(0, nhspCode);
								break;
							case "order_s_wxsp_code":
								bean.setWxspCode(wxspCode[0]);wxspCode=ArrayUtils.delete(0, wxspCode);
								break;
							case "order_s_cust_po_no":
								bean.setCustPoNo(custPoNo[0]);custPoNo=ArrayUtils.delete(0, custPoNo);
								break;
							case "order_s_remark":
								bean.setRemark(remark[0]);remark=ArrayUtils.delete(0, remark);
								break;
							case "order_s_glasses_type":
								bean.setGlassesType(glassesType[0]);glassesType=ArrayUtils.delete(0, glassesType);
								break;
							case "order_s_light_trans":
								bean.setLightTrans(lightTrans[0]);lightTrans=ArrayUtils.delete(0, lightTrans);
								break;
							case "order_s_location":
								bean.setLocation(location[0]);location=ArrayUtils.delete(0, location);
								break;
							case "order_s_other_color":
								bean.setOtherColor(otherColor[0]);otherColor=ArrayUtils.delete(0, otherColor);
								break;
							case "order_s_content":
								bean.setContent(content[0]);content=ArrayUtils.delete(0, content);
								break;
							case "order_s_size":
								bean.setSize(size[0]);size=ArrayUtils.delete(0, size);
								break;
							case "order_s_quality_type":
								bean.setQualityType(qualityType[0]);qualityType=ArrayUtils.delete(0, qualityType);
								break;
							case "order_s_font":
								bean.setFont(font[0]);font=ArrayUtils.delete(0, font);
								break;
							case "order_s_arts":
								bean.setArts(arts[0]);arts=ArrayUtils.delete(0, arts);
								break;
							default:
								break;
							}
					}
					bean.setLineNo(lineNo[i]);
					//投料数外部有 取外部 外部没有取内部
					if(bean.getSmallFeedTotal()!=null) {//内部有投料数
						bean.setFeedTotal(bean.getSmallFeedTotal());
					}else if(feedTotal!=null) {//内部没有 外部有
						bean.setFeedTotal(feedTotal[i].trim().length()>0?feedTotal[i]:null);
					}else {//内部外部都没有
						bean.setFeedTotal(bean.getSmallFeedTotal());
					}
					//订单数外部有 取外部 外部没有取内部
					if(bean.getSmallOrderTotal()!=null) {//内部有订单数
						bean.setOrderTotal(bean.getSmallOrderTotal());
					}else if(orderTotal!=null) {//内部没有订单数  外部有
						bean.setOrderTotal(orderTotal[i].trim().length()>0?orderTotal[i]:null);
					}else {//内外都没有
						bean.setOrderTotal(bean.getSmallOrderTotal());
					}
					
					bean.setOrderNo(orderNo);
					if(materialFactory==null) {//旧模版默认为原材料仓
						bean.setMaterialFactory("1");
					}else {
						bean.setMaterialFactory(materialFactory[j]);
					}
					bean.setMaterialType(materialType[j]);
					list.add(bean);
				}
			}
		}
		
		/*int index = 0;
		for(int i=0;i<lineNo.length;i++) {
			for(int j=0;j<tableSourceArr.length;j++) {
				int index_data=i*tableSourceArr.length+j;
				bean = new OrderInfoDetailBean();
				bean.setColor(materialColor[index_data]);
				bean.setColorDescription(materialColorDescription[index_data]);
				bean.setFeedTotal(feedTotal[i]);
				bean.setLineNo(lineNo[i]);
				bean.setMaterialCode(materialCode[index_data]);
				bean.setMaterialFactory("1");
				if("2".equals(tableSourceArr[j])){//无规格
					bean.setMaterialModel("");
				}else{
					bean.setMaterialModel(materialModel[index]);
					index++;
				}
				bean.setMaterialType(materialType[j]);
				bean.setOrderNo(orderNo);
				bean.setOrderTotal(orderTotal[i]);
				System.out.println(bean);
				list.add(bean);
			}
		}*/
		return list;
	}

	//获取固定值
	public Map<String, String> getOrderInfoFixed(Map map) {
		// TODO Auto-generated method stub
		map = orderCreateMapper.getOrderInfoFixed(map);
		return map;
	}

	public JSONObject getOrderInfoForTemp(Map<String, String> map) {
		Map<String,String> map_orderInfo = orderCreateMapper.getOrderInfoByOrderNo(map.get("order_no").toString());
		List<Map<String, String>> map_orderInfoDetail =orderCreateMapper.getOrderInfoDetailByOrderNo(map.get("order_no").toString());
		List<Map<String, Object>> map_orderInfoTail =orderCreateMapper.getOrderInfoTailByOrderNo(map.get("order_no").toString());
		Map<String,String> map_orderCustomTag = orderCreateMapper.getOrderCustomTagByOrderNo(map.get("order_no").toString());

		JSONObject object = new JSONObject();
		object.put("orderInfo", map_orderInfo);//订单信息主表
		object.put("orderInfoDetail", map_orderInfoDetail);//订单信息明细表
		if (map.get("sort_order_s_input_material_type")!=null) {
			List<Map<String, Object>> sort_map_orderInfoTail=getSortMapOrderInfoTail(map,map_orderInfoTail);
			object.put("orderInfoTail", sort_map_orderInfoTail);
		}else {
			object.put("orderInfoTail", map_orderInfoTail);
		}
		object.put("orderCustomTag", map_orderCustomTag);
		return object;
	}
	/*
	 * 流程卡特有 sort_order_s_input_material_type
	 * 通过排序避免数据顺序问题导致流程卡数据赋值出现顺序问题
	 * */
	private List<Map<String, Object>> getSortMapOrderInfoTail(Map<String, String> map,List<Map<String, Object>> source_map_orderInfoTail) {
		List<Map<String, Object>> sort_map_orderInfoTail = new ArrayList<>();
		String[] order_s_input_material_house_arr=map.get("order_s_input_material_house").toString().split(",");
		String[] order_s_input_material_type_arr =map.get("order_s_input_material_type").toString().split(",");
		Map<String, Object> temp_map=null;
		for(int i=0;i<order_s_input_material_house_arr.length;i++) {
			temp_map=new HashMap<>();
			temp_map.put("order_s_input_material_code", " ");
			temp_map.put("order_s_input_material_house", order_s_input_material_house_arr[i]);
			temp_map.put("order_s_input_material_type", order_s_input_material_type_arr[i]);
			temp_map.put("order_s_input_val_material_type", "");
			sort_map_orderInfoTail.add(i,temp_map);
			for(Map<String, Object> source_map:source_map_orderInfoTail) {
				if (source_map.get("order_s_input_material_house").toString().equals(order_s_input_material_house_arr[i].toString())
						&&
						source_map.get("order_s_input_material_type").toString().equals(order_s_input_material_type_arr[i].toString())	) {
					System.out.println(source_map.get("order_s_input_material_type").toString());
					sort_map_orderInfoTail.remove(i);
					sort_map_orderInfoTail.add(source_map);
					break;
				}
			}
		}
		
		return sort_map_orderInfoTail;
	}

	public int checkTempAndOrderNo(Map<String, Object> map) {
		return orderCreateMapper.checkTempAndOrderNo(map);
	}
	//查询订单是否已审核
	public int getAuditTotalByOrderNo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getAuditTotalByOrderNo(map);
	}
	//更新订单 --审核订单使用
	public int updateAudit(Map map) {
		// TODO Auto-generated method stub
		if("1".equals(map.get("order_p_audit").toString())) {
			map.put("order_p_audit", "1");
			map.put("order_p_order_status", "审核");
			map.put("status", "1");
		}else if("0".equals(map.get("order_p_audit").toString())){
			map.put("order_p_audit", "0");
			map.put("order_p_order_status", "计划");
			map.put("status", "0");
		}else{
			map.put("order_p_audit", "-1");
			map.put("order_p_order_status", "退回");
			map.put("status", "0");
		}
		map.put("order_no", map.get("order_p_order_no").toString());
		orderCreateApplyService.sendTemplateMsg("audit",map);
		return orderCreateMapper.updateAudit(map);
	}
	
	public int deleteTemplate(Map<String, Object> map) {
		return orderCreateMapper.deleteTemplate(map);
	}
	//获取部门信息表
	public List<Map> selectOnlineByType(Map map) {
		// TODO Auto-generated method stub
		if("order_p_deptid".equals(map.get("type"))) {
			return orderCreateMapper.selectOnlineByDeptId(map);
		}
		if("order_p_product_work_no".equals(map.get("type"))) {
			return orderCreateMapper.selectOnlineForproductWorkNo(map);
		}
		return null;
	}
	//通过参数获取生产订单主表数据
	public List<Map> getOrderInfoByParam(Map map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getOrderInfoByParam(map);
	}

	public int deleteOrderInfoPrimary(Map map) {
		// TODO Auto-generated method stubupdateAudit
		return orderCreateMapper.updateOrderInfoPrimary(map);
	}

	public Map<String, String> getOrderInfoFixedPermission(Map map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getOrderInfoFixedPermission(map);
	}
	//订单添加操作日志
	public void operateLog(Map map) {
		orderCreateMapper.operateLog(map);
	}
	
	public List<Map> getOrderNoByBussNo(Map map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getOrderNoByBussNo(map);
	}

	/** 订单主表对应关系
	 * order_p_document_no	单据编号
		order_p_order_total	订单总数
		order_p_order_type	订单类型
		order_p_prod_type	产品型号
		order_p_pic_no	图号
		order_p_custmer	客户
		order_p_order_no	订单编号
		order_p_product_work_no	生产车间
		order_p_quality	品质
		order_p_process_no	流程号
		order_p_cust_no	客户编号
		order_p_cust_order_no	客户订单号
		order_p_place_order_time	下单日期
		order_p_created	制单
		order_p_product_size	生产尺寸
		order_p_version_no	客户型号
		order_p_product_no	工厂型号
		order_p_delivery_time	交货日期
		order_p_auditor	审核人
		order_p_word_size	印字尺寸
		order_p_cust_remark	客户要求
		order_p_pic_quan	圈图
		order_p_pic_tui	腿图
		order_p_mp_pic_left	镜片印字_左贴纸
		order_p_mp_pic_right	镜片印字_右贴纸
		order_p_mp_t	镜片印字_圈厚度
		order_p_mp_groove	镜片印字_圈槽
		order_p_mp_qztc	镜片印字_圈庄头长
		order_p_mp_qwd	镜片印字_圈弯度
		order_p_mp_kqcc	镜片印字_开圈尺寸
		order_p_mp_qpl	镜片印字_圈拼料
		order_p_mp_tygg	镜片印字_托叶规格
		order_p_fp_zjsnc	脚丝印字_左脚丝内侧
		order_p_fp_zjwnc	脚丝印字_左脚尾内侧
		order_p_fp_yjsnc	脚丝印字_右脚丝内侧
		order_p_fp_yjwnc	脚丝印字_右脚尾内侧
		order_p_fp_eye_size	脚丝印字_眼核尺寸
		order_p_fp_qhs	脚丝印字_圈花式
		order_p_fp_yz	脚丝印字_叶子
		order_p_fp_qqzcd	脚丝印字_前圈总长度
		order_p_fp_tpl	脚丝印字_腿拼料
		order_p_fp_ctcc	脚丝印字_冲腿尺寸
		order_p_fp_cdh	脚丝印字_冲刀号
		order_p_fp_ccxh	脚丝印字_插针型号
		order_p_packing	包装
		order_p_ztcd	庄头长度
		order_p_ltwj	两腿尾距
		order_p_mp_ls	镜片印字_螺丝
		order_p_order_make_time	制单时间
		order_p_product_type	产品类型
		order_p_order_status	单据状态
		order_p_audit_time	审核时间
		order_p_qrcode	二维码
		order_p_audit	审核
		order_p_company	公司
		order_p_product_line	生产线
	 */
	
	
	
	
	
	
}
